﻿$(document).ready(function () {
    LoadDirectory();
});

function LoadDirectory(path) {
    $('.loading').show();
    $('#path').val(path);
    $.post("/lidocaine.ashx?f=LoadExplorer", { "path": (path == null ? "" : path), "depth": (path == null ? "0" : path.split('/').length) }, function (data) {
        var oJSON = eval("(" + data + ")");
        var output = '';
        output += "<div class=\"breadcrumbs\"><a class=\"address\" href=\"#\" onclick=\"LoadDirectory();return false;\"><span class=\"icon sprite-Globe\"></span>" + oJSON[0].N + "</a>";
        if (path != null && path != '') {
            var nodes = path.split('/');
            var rpath = '';
            for (var i = 0; i < nodes.length; i++) {
                rpath += nodes[i];
                output += "<a class=\"address\" href=\"#\" onclick=\"LoadDirectory('" + rpath + "');return false;\"><span class=\"icon sprite-Folder\"></span>" + nodes[i] + "</a>";
                rpath += "/";
            }
        }

        output += "</div>";
        $('#ribbon').html(output);

        output = '';
        for (var k = 0; k < oJSON[0].Items.length; k++) {
            switch (oJSON[0].Items[k].Source) {
                case "1":
                    output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" title=\"Click To Open\" class=\"aFilename\" onclick=\"LoadDirectory('" + oJSON[0].Items[k].Path + "'); return false;\"><span class=\"icon sprite-Folder\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                    break;
                case "2":
                    output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"window.opener.CKEDITOR.tools.callFunction(" + getParameterByName("CKEditorFuncNum") + ", '/" + oJSON[0].Items[k].Path + "');window.close();return false;\" class=\"aFilename\" title=\"Click To View\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                    break;
                case "3":
                    output += "<div class=\"item" + (k % 2 == 0 ? " alt" : "") + "\"><a href=\"#\" onclick=\"window.opener.CKEDITOR.tools.callFunction(" + getParameterByName("CKEditorFuncNum") + ", '/" + oJSON[0].Items[k].Path + "');window.close();return false;\" class=\"aFilename\" title=\"Click To View\"><span class=\"icon sprite-New\"></span>" + oJSON[0].Items[k].Path.substring(oJSON[0].Items[k].Path.lastIndexOf('/') + 1) + "</a></div>";
                    break;
            }
        }

        $('#explorer').html(output);
        $('.loading').hide();
    });
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
